<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
use Common\Model\NewsModel;
use Common\Model\NewsCategoryModel;
class NewsController extends  CommonController{

	public function index(){
		$model = new NewsModel();
		$data = $model->getList();
		$url = U("admin/news/index");
		$page = PageShow($data['count'],$url);

		$this->assign('data',$data)->assign('page',$page);
		$this->display();
	}


	public function add(){
		if(IS_POST){
			$model = new NewsModel();
			if(!$model->createNews()){
				$this->error($model->getError());
			}
			$this->success('添加成功',U("admin/news/index"));
		}
		$cateModel = new NewscategoryModel();
		$cateList = $cateModel->getCategoryList();
		$this->assign('cateList',$cateList);
		$this->display();
	}

	public function edit(){
		$id = I('id');
		$model = new NewsModel();
		if(IS_POST){
			if(!$model->edit()){
				$this->error($model->getError());
			}
			$this->success('编辑成功',U("admin/news/index"));
		}else{
			$data = $model->find($id);
			$data['content'] = htmlspecialchars_decode($data['content']);
			$cateModel = new NewscategoryModel();
			$cateList = $cateModel->getCategoryList();
			$this->assign('cateList',$cateList);
			$this->assign('data',$data);
		}
		$this->display();
	}

	public function del(){
		$id = I('get.id');
		if(!$id) $this->error('id不能为空');
		$model = new NewsModel();
		$res = $model->del();
		if($res){
			$this->success("删除成功",U("admin/news/index"));
		}else{
			$this->error("删除失败,请重试");
		}
	}

	public function pass(){
		$id = I("get.id");
		if(!$id) $this->error('id不能为空');
		$model = new NewsModel();
		if(!$model->pass()){
			$this->error($model->getError());
		}
		$this->success('处理成功',U("admin/news/index"));
	}
}
